﻿
//初始化地图对象，加载地图  
function mapInit() {
    
    mapObj = new AMap.Map("Map", { center: new AMap.LngLat(x, y),
        level: 17
    });
    
    var clickEventListener = AMap.event.addListener(mapObj, 
        'click', function (event) 
        {
            var e = event? event : window.event;
            if (marker0 == undefined) 
            {
                marker0 = new AMap.Marker(
                {
                    position: e.lnglat,
                    draggable: true //点标记可拖拽                  
                });

                document.getElementById("lngX").value = e.lnglat.getLng();
                document.getElementById("latY").value = e.lnglat.getLat();
                marker0.setMap(mapObj);
                mapObj.bind(marker0, 
                    "dragend", function (e) 
                    {

                        document.getElementById("lngX").value = e.lnglat.getLng();
                        document.getElementById("latY").value = e.lnglat.getLat();
                    });
            }
        });

        addBuildings();
}



//添加三维建筑
function addBuildings() {
    
    if (typeof (Worker) !== "undefined") {
        var buildings = new AMap.Buildings(); //实例化3D楼块图层  
        buildings.setMap(mapObj); //在map中添加3D楼块图层  
        
    } else {
        document.getElementById('Message').textContent="您的浏览器不支持HTML5的部分功能！";
    }
}





function placeSearch() {
    
    var MSearch;
    mapObj.plugin(["AMap.PlaceSearch"], function() {        
        MSearch = new AMap.PlaceSearch({ //构造地点查询类
            pageSize:10,
            pageIndex:1,
            city:"021" //城市
        }); 
        AMap.event.addListener(MSearch, "complete", keywordSearch_CallBack);//返回地点查询结果
        var keyword=document.getElementById('SearchKeyWordId').value;
        MSearch.search(keyword); //关键字查询
    });
}





//添加marker&infowindow    
function addmarker(i, d) {
    var lngX = d.location.getLng();
    var latY = d.location.getLat();
    var markerOption = {
        map:mapObj,
        icon:"http://webapi.amap.com/images/" + (i + 1) + ".png",
        position:new AMap.LngLat(lngX, latY)
    };
    var mar = new AMap.Marker(markerOption);
    // marker=[];
    // windowsArr=[];

    marker.push(new AMap.LngLat(lngX, latY));

    var infoWindow = new AMap.InfoWindow({
        content:"<h3><font color=\"#00a6ac\">&nbsp;&nbsp;" + (i + 1) + ". " + d.name + "</font></h3>" + TipContents(d.type, d.address, d.tel),
        size:new AMap.Size(300, 0), 
        autoMove:true,  
        offset:new AMap.Pixel(0,-30)
    });
    windowsArr.push(infoWindow); 
    var aa = function (e) {infoWindow.open(mapObj, mar.getPosition());};
    AMap.event.addListener(mar, "click", aa);
}
//回调函数



function keywordSearch_CallBack(data) {
    var resultStr = "";
    var poiArr = data.poiList.pois;
    var resultCount = poiArr.length;
        marker0 =undefined;
    if (marker.length!=0)
        {  var n=marker.length;
            for (var i = n - 1; i >= 0; i--) {
               //alert(marker[i]);
               marker.pop();
               windowsArr.pop();
            };
            mapObj.removeOverlays();
            
        }

    for (var i = 0; i < resultCount; i++) {
        resultStr += "<div id='divid" + (i + 1) + "' onmouseover='openMarkerTipById1(" + i + ",this)' onmouseout='onmouseout_MarkerStyle(" + (i + 1) + ",this)' style=\"font-size: 12px;cursor:pointer;padding:0px 0 4px 2px; border-bottom:1px solid #C1FFC1;\"><table><tr><td><img src=\"http://webapi.amap.com/images/" + (i + 1) + ".png\"></td>" + "<td><h3><font color=\"#00a6ac\">名称: " + poiArr[i].name + "</font></h3>";
            resultStr += TipContents(poiArr[i].type, poiArr[i].address, poiArr[i].tel) + "</td></tr></table></div>";
            addmarker(i, poiArr[i]);
    }
    mapObj.setFitView();
    document.getElementById("result").innerHTML = resultStr;
}



function TipContents(type, address, tel) {  //窗体内容
    if (type == "" || type == "undefined" || type == null || type == " undefined" || typeof type == "undefined") {
        type = "暂无";
    }
    if (address == "" || address == "undefined" || address == null || address == " undefined" || typeof address == "undefined") {
        address = "暂无";
    }
    if (tel == "" || tel == "undefined" || tel == null || tel == " undefined" || typeof address == "tel") {
        tel = "暂无";
    }
    var str = "&nbsp;&nbsp;地址：" + address + "<br />&nbsp;&nbsp;电话：" + tel;// + " <br />&nbsp;&nbsp;类型：" + type;
    return str;
}



function openMarkerTipById1(pointid, thiss) {  //根据id 打开搜索结果点tip
    thiss.style.background = '#CAE1FF';
    windowsArr[pointid].open(mapObj, marker[pointid]);
}



function onmouseout_MarkerStyle(pointid, thiss) { //鼠标移开后点样式恢复
    thiss.style.background = "";
}


//----------------------菜单开关---------------------------//
function Switch(i) {
    MenuColse();
    MenuOpen(i);
    var dropMenuId = document.getElementById("Menu-buttom-map");
    if (dropMenuId.style.display == "block")
        mapInit();
}
function MenuColse() {
    var n = MenuStr.length;
    for (var j = 0; j < n; j++) {
        var dropMenuId = document.getElementById(MenuStr[j]);
        if (dropMenuId.style.display == "block")
            dropMenuId.style.display = "none";
    }
}
function MenuOpen(i) {
    var dropMenuId = document.getElementById(MenuStr[i]);
    dropMenuId.style.display = "block";
}
//////////////////////////////////////////////////////////////